const { $Toast } = require('../../../utils/iview/base/index');
const Tools = require('../../../utils/modules/Tools');
import NetWork from '../../../utils/modules/NetWork';

const app = getApp();


Page({
  data: {
    auth: true,
    isloade: {
      hidden: false
    },
    // diabled: false,
    index:0,
    _baseUrl: app.globalData._baseUrl,
    _imgPath: app.globalData._imgPath,
    _imgPaths: app.globalData._imgPaths,
    _backImg: app.globalData._backImg,
    img: [],
    user_type: 1,
    designer_id: '',
    openid: app.globalData.openid,
    shopName: '',
    shopId: '',
    userInfo: '',
    user: {},
    taocan: [],
  },
  onLoad(options) {
    var _that = this;
    var { type } = options;

 
      _that.setTitle('会员充值');
    var user = wx.getStorageSync('user');
    var userInfo = wx.getStorageSync('userInfo');
    var { user } = _that.data;
    _that.setData({
      user: user,
      userInfo: userInfo
    });

  },
  setIndex(e){
    var {index} = e.currentTarget.dataset;
    var _that = this;
    _that.setData({
      indexs:index
     
    })
    console.log(_that.data.indexs)
  },
  onShow: function () {
    var _that = this
    _that.getcharge();
    _that.setData({
      isclick:0
    })

  },


  scanCode: function () {
    var that = this;
    wx.scanCode({ //扫描API
      success(res) { //扫描成功
        console.log(res) //输出回调信息
        that.setData({
          scanCodeMsg: res.result
        });
        wx.showToast({
          title: '成功',
          duration: 1000
        })
      }
    })
  },


  

  
  // },
  /* 修改头部标题 */
  setTitle(str) {
    wx.setNavigationBarTitle({
      title: str,
      success: function () {

      }
    })
  },
  setType(e) {
    var index = e.target.dataset.index;
    this.setData({
      roomType: index
    })
  },
  // onShareAppMessage(str) {
  // 	return {
  // 		title: str,
  // 		path: this.route
  // 	}
  // },
  isUserInfo() {
    var _that = this;
    wx.getSetting({
      success(res) {
        // console.log(res)
        if (!res.authSetting['scope.userInfo']) {
          _that.setData({
            auth: false
          })

        }
      }
    })
  },

  userInfo(e) {

    let _that = this;
    let _userInfo = e.detail.userInfo;
    console.log(_userInfo)
    wx.setStorageSync('_userInfo', _userInfo);
    let net = new NetWork(app.globalData._baseUrl);
    let _data = e.detail;


    if (_userInfo) {
      this.setData({
        auth: true,
      })
    }


    wx.login({
      success(res) {
        if (res.code) {
          console.log(res.code, "code");

          net._request({
            url: 'api/v1/login/mini',
            method: 'POST',
            data: {
              code: res.code,
              rawData: _data.rawData,
              signature: _data.signature,
              iv: _data.iv,
              encryptedData: _data.encryptedData
            },
          }, '', res => {
            app.globalData.user = _userInfo;
            var user = res.data.data;
            wx.setStorageSync('userInfo', user);

            _that.setData({
              userInfo: user
            })

            wx.setStorageSync('user', {
              user_type: 1
            });
            $Toast({
              content: res.data.msg,
              type: 'success'
            });

          }, res => {
            console.log("失败返回", res);
            app.globalData.user = _userInfo;
            var user = res.data.data;
            wx.setStorageSync('userInfo', user);
            $Toast({
              content: res.data.msg,
              type: 'success'
            });
          });
        }
      }
    })
  },



  //获取会员充值
  recharge() {
    let _that = this;

    // 现在isclick等于0
    var isclick = _that.data.isclick;

    //isclick是0为假，假就跳出这个循环
    if(isclick){
      return;
    }

    // 开始进入这个循环，就开始调取支付接口，
    _that.setData({
      isclick:1
    })

    //支付调取接口成功后5秒再把isclick转变为0，然后在以此类推进入循环
    var token = wx.getStorageSync('token');
    setTimeout(() => {
      _that.setData({
        isclick: 0
      })
    }, 5000)
    
    let net = new NetWork(_that.data._baseUrl)
    net._request({
      url: 'api/v1/member/member_recharge',
      data: {
        token: token, recharge_id: _that.data.indexs,
      },
      method: 'POST'
    }, '', res => {

      // console.log(res.data.data, "res")
      var tem = res.data.data
      //只是支付接口，支付的功能的接口在那里就写在那个接口的res中
      //这个部分是按了确定才出现支付的， 所以写在这里
      wx.requestPayment({
        timeStamp: tem.timeStamp,
        nonceStr: tem.nonceStr,
        package: tem.packages,
        signType: tem.signType,
        paySign: tem.paySign,
        success(res) { 
          $Toast({
            content: '支付成功',
            type: 'success'
    
          });
          setTimeout(() => {
            wx.navigateBack({
              changed: true
            });
          }, 1000)

        },
        fail(res) { 
          $Toast({
            content: '支付失败',
            type: 'success'
          });
      
        }
      })    
    }, res => {
      if (res.data.error_code == 400 && res.data.msg == 'token无效或已过期') {
        app.refreshtoken(token);
        setTimeout(() => {
          _that.recharge();
        }, 1500)
      }
    });
      
  },


  //获取充值套餐ID
  getcharge() {
    let _that = this;
    var token = wx.getStorageSync('token');
    // var indexs = _that.data.index ;

    let net = new NetWork(_that.data._baseUrl)
    net._request({

      url: 'api/v1/member/get_recharge_menu',
      data: {
        token: token, 
      },
      method: 'POST'
    }, '', res => {
      console.log(res.data.data, "res1");
      // 对应着代码里面的循环 
      _that.setData({
        taocan: res.data.data
      })
    }, res => {
      if (res.data.error_code == 400) {
        app.refreshtoken(token);
        setTimeout(() => {
          _that.getcharg();
        }, 1500)
      }
    });
  },


})